<template>
  <div class="container">
    <div class="app-container" style="padding: 20px;">
      <el-row class="header" style="font-size: 14px; display: flex; align-items: center;">
        <span>员工姓名：</span>
        <el-input
          v-model="searchForm.employeeName"
          placeholder="请输入员工姓名"
          size="mini"
          style="width: 180px; margin-right: 10px;"
        />
        <el-button size="mini" type="primary" @click="searchEmployees">查询</el-button>
        <el-button size="mini" type="success" @click="showAddEmployeeDialog">添加员工</el-button>
      </el-row>
      <el-divider />
      <el-table :data="employeesList" style="width: 100%; margin-top: 20px;">
        <el-table-column type="index" label="序号" width="80" />
        <el-table-column prop="name" label="员工姓名" />
        <el-table-column prop="account" label="登录账号" />
        <el-table-column prop="contact" label="联系方式" />
        <el-table-column prop="role" label="角色" />
        <el-table-column prop="status" label="状态" />
        <el-table-column prop="addTime" label="添加时间" />
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <el-button size="mini" @click="editEmployee(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="deleteEmployee(scope.row.id)">删除</el-button>
            <el-button size="mini" @click="resetPassword(scope.row)">重置密码</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        layout="total, prev, pager, next"
        :total="totalEmployees"
        :page-size="pageSize"
        style="text-align: right; margin-top: 20px;"
        @current-change="handlePageChange"
      />
    </div>

    <!-- 添加/编辑员工对话框 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible">
      <el-form :model="form" label-width="120px">
        <el-form-item label="员工姓名">
          <el-input v-model="form.name" size="mini" />
        </el-form-item>
        <el-form-item label="登录账号">
          <el-input v-model="form.account" size="mini" />
        </el-form-item>
        <el-form-item label="联系方式">
          <el-input v-model="form.contact" size="mini" />
        </el-form-item>
        <el-form-item label="角色">
          <el-select v-model="form.role" size="mini">
            <el-option label="员工及系统管理" value="employee" />
            <el-option label="超级管理员" value="admin" />
            <el-option label="行车管理" value="manager" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="dialogVisible = false">取消</el-button>
        <el-button size="mini" type="primary" @click="saveEmployee">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchForm: {
        employeeName: ''
      },
      employeesList: [],
      totalEmployees: 0,
      pageSize: 10,
      currentPage: 1,
      dialogVisible: false,
      dialogTitle: '添加员工',
      form: {
        id: '',
        name: '',
        account: '',
        contact: '',
        role: ''
      }
    }
  },
  created() {
    this.fetchEmployees()
  },
  methods: {
    showAddEmployeeDialog() {
      this.dialogTitle = '添加员工'
      this.resetForm()
      this.dialogVisible = true
    },
    editEmployee(employee) {
      this.dialogTitle = '编辑员工'
      this.form = Object.assign({}, employee)
      this.dialogVisible = true
    },
    deleteEmployee(id) {
      this.$confirm('确定要删除这条记录吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 调用删除员工的 API
        console.log('删除员工:', id)
      })
    },
    resetPassword(employee) {
      // 调用重置密码的 API
      console.log('重置密码员工:', employee)
    },
    saveEmployee() {
      // 调用保存员工的 API
      console.log('保存员工:', this.form)
      this.dialogVisible = false
    },
    resetForm() {
      this.form = {
        id: '',
        name: '',
        account: '',
        contact: '',
        role: ''
      }
    },
    handlePageChange(page) {
      this.currentPage = page
      this.fetchEmployees()
    },
    searchEmployees() {
      // 调用搜索员工的 API
      console.log('搜索员工:', this.searchForm)
      this.fetchEmployees()
    },
    fetchEmployees() {
      // 调用获取员工列表的 API
      console.log('获取员工列表:', this.currentPage)
    }
  }
}
</script>

<style scoped>
.container {
  padding: 20px;
}

.dialog-footer {
  text-align: right;
}

.header {
  margin-bottom: 20px;
}
</style>
